import cv2
import numpy as np


def get_transform(mat_src, mat_dst):
    print(mat_src)
    print(mat_dst)
    img = np.zeros((600, 600, 3), dtype=np.uint8)
    img[50:100, 50:100] = 255
    img[350:400, 350:400] = 255

    height, width = img.shape[:2]

    x1 = int((mat_src[0, 0] + mat_src[1, 0]) * 0.5)
    y1 = int((mat_src[0, 1] + mat_src[1, 1]) * 0.5)

    cv2.circle(img, (int(x1), int(y1)), 3, (0, 0, 255), -1)

    cv2.line(img, (0, 0), (50, 100), (0, 255, 0), 1)  # 绿色，3个像素宽度
    cv2.line(img, (300, 50), (50, 100), (0, 255, 0), 1)  # 绿色，3个像素宽度
    mat_trans = cv2.getAffineTransform(mat_src, mat_dst)

    print(mat_trans)
    dst = cv2.warpAffine(img, mat_trans, (width, height))

    return mat_trans, dst, img


def transform_point(mat_trans, point):  # point x y

    data = np.dot(mat_trans, np.asarray([point[0], point[1], 1]))
    return data


def rotate_img_original(theta):
    # 将角度转换为弧度制
    radian_theta = theta / 180 * np.pi
    # 定义围绕原点旋转的变换矩阵
    M2 = np.array([[np.cos(radian_theta), np.sin(radian_theta), 0],
                  [-np.sin(radian_theta), np.cos(radian_theta), 0]])
    # 读取图像
    img2 = cv2.imread("../resource/img_10.png")
    # 定义旋转后图片的宽和高
    height, width = img2.shape[:2]
    # 将图片由BGR转为RGB
    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
    # 围绕原点逆时针旋转\theta度
    rotate_img = cv2.warpAffine(img2, M2, (width, height))
    # 显示图像
    cv2.imshow("dst", rotate_img)
    cv2.waitKey(0)


if __name__ == '__main__':
    mat_src = np.float32([[0, 0], [10, 10], [200, 200]])
    mat_dst = np.float32([[0, 0], [0, 10], [0, 200]])
    mat_trans, dst, img = get_transform(mat_src, mat_dst)

    x1 = 10
    y1 = 10

    data = transform_point(mat_trans, [x1, y1])

    x2 = data[0]
    y2 = data[1]
    print(x2, y2)

    rotate_img_original(45)
    cv2.circle(dst, (int(x2), int(y2)), 2, (0, 0, 255), -1)
    # 显示
    imgs = np.hstack([img, dst])
    # imgs = np.hstack([img, dst])
    # cv2.namedWindow("imgs", cv2.WINDOW_NORMAL)
    cv2.imshow("img_o", img)
    cv2.imshow("dst", dst)
    # cv2.imshow("imgs", imgs)
    cv2.waitKey(0)
